<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ MCP SDK/资源使用</title>
        <meta name="description" content="轻量级的 Java 8 Model Context Protocol (MCP) SDK. 资源使用"/>
        <meta name="keywords" content="mcp, mcp sdk, java mcp, mcp client, mcp server, java8 mcp, 资源使用"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" href="/asset/style/main.css"/>
        <link rel="icon" type="image/png" href="/asset/favicon.ico"/>
        <script src="/asset/common.js"></script>
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/">🏠 首页</a>
                    | ⚙️ 源码:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-mcp">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-mcp">Gitcode</a>
                    |
                    <a href="/">英文版本</a>
                </div>
                <h1>AJ MCP SDK</h1>
                <h3>用户手册</h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/cn">首页</a>
                    </li>
                    <li>
                        <a href="/architecture/index-cn">架构原理</a>
                    </li>
                </ul>
                <h3>MCP 客户端 SDK</h3>
                <ul>
                    <li>
                        <a href="/client/setup-client-cn">安装与配置</a>
                    </li>
                    <li>
                       <a href="/client/initialization-cn">初始化</a>
                    </li>
                    <li>
                       <a href="/client/resource-cn">Resources 用法</a>
                    </li>
                     <li>
                       <a href="/client/prompt-cn">Prompts 用法</a>
                    </li>
                     <li>
                       <a href="/client/tool-cn">Tools 用法</a>
                    </li>
                </ul>
                <h3>MCP 服务端 SDK</h3>
                <ul>
                      <li>
                           <a href="/server/usage-server-cn">Server SDK 用法指南</a>
                      </li>
                      <li>
                           <a href="/server/res-server-cn">Resources 用法</a>
                      </li>
                      <li>
                           <a href="/server/pro-server-cn">Prompts 用法</a>
                      </li>
                      <li>
                             <a href="/server/tool-server-cn">Tools 用法</a>
                       </li>
                      <li>
                         <a href="/server/sample-cn">整合例子</a>
                      </li>
                </ul>

                <h3>其他</h3>
                <ul>
                    <li><a href="/misc/versions">版本记录</a></li>
                    <li><a href="/misc/contact-cn">联系方式</a></li>
                </ul>
            </menu>
            <article>
                <h2>资源（Resource）使用</h2>
<p>资源系统使 LLM 驱动的应用能够通过统一接口访问结构化内容。每个资源通过 URI 标识，可以表示任何对 LLM 有用的内容类型。AJ MCP 客户端提供发现和访问服务器端资源的方法。</p>
<h3>列出资源</h3>
<p>列出所有可用资源：</p>
<pre><code class="language-java">List&lt;ResourceItem&gt; resourceList = mcpClient.listResources();
assertEquals(2, resourceList.size(), &quot;期望恰好有两个资源&quot;);

ResourceItem blob = getResourceRef(&quot;blob&quot;, resourceList);
assertNotNull(blob, &quot;资源 'blob' 不应为 null&quot;);
assertEquals(&quot;blob&quot;, blob.getName(), &quot;资源名不符（'blob'）&quot;);
assertEquals(&quot;file:///blob&quot;, blob.getUri(), &quot;URI 不符（'blob'）&quot;);
assertEquals(&quot;image/jpg&quot;, blob.getMimeType(), &quot;MIME 类型不符（'blob'）&quot;);
assertEquals(&quot;A nice pic&quot;, blob.getDescription(), &quot;描述不符（'blob'）&quot;);
</code></pre>
<p>此方法将跨多个页面获取所有的资源，并没有分页。如果你需要对分页进行更精细的控制，可以改用重载的<code>listResources(int pageNo)</code>方法。</p>
<pre><code class="language-java">List&lt;ResourceItem&gt; resourceList = mcpClient.listResources(1);
assertEquals(2, resourceList.size(), &quot;Expected exactly two resources&quot;);
</code></pre>
<h3>读取资源内容</h3>
<p>读取某个资源内容：</p>
<pre><code class="language-java">GetResourceResult.ResourceResultDetail response = mcpClient.readResource(&quot;file:///text&quot;);
assertEquals(1, response.getContents().size(), &quot;期望内容数量为 1&quot;);

ResourceContent contents = response.getContents().get(0);

ResourceContentText textContents = (ResourceContentText) contents;
assertEquals(&quot;file:///text&quot;, textContents.getUri(), &quot;URI 应为 'file:///text'&quot;);
assertEquals(&quot;text888&quot;, textContents.getText(), &quot;文本内容应为 'text888'&quot;);
</code></pre>
<p><code>readResource()</code> 方法返回 <code>ResourceResultDetail</code>，其中包含资源内容数组。每项内容类型根据 MIME 类型决定，可能是文本内容，也可能是二进制内容。</p>
<pre><code class="language-java">GetResourceResult.ResourceResultDetail response = mcpClient.readResource(&quot;file:///blob&quot;);
assertEquals(1, response.getContents().size(), &quot;期望内容数量为 1&quot;);

ResourceContent contents = response.getContents().get(0);

ResourceContentBinary blobContents = (ResourceContentBinary) contents;
assertEquals(&quot;file:///blob&quot;, blobContents.getUri(), &quot;URI 应为 'file:///blob'&quot;);
assertEquals(&quot;blob&quot;, blobContents.getBlob(), &quot;二进制内容应为 'blob'&quot;);
</code></pre>
<p>你也可以通过模板资源读取内容，即可传递参数给资源：</p>
<pre><code class="language-java">GetResourceResult.ResourceResultDetail response = mcpClient.readResource(&quot;file:///text-template/hello&quot;);
assertEquals(1, response.getContents().size(), &quot;期望内容数量为 1&quot;);

ResourceContent contents = response.getContents().get(0);

ResourceContentText textContents = (ResourceContentText) contents;
assertEquals(&quot;file:///text-template/hello&quot;, textContents.getUri(), &quot;URI 应为 'file:///text-template/hello'&quot;);
assertEquals(&quot;text hello&quot;, textContents.getText(), &quot;文本内容应为 'text hello'&quot;);
</code></pre>
<!--
### 资源订阅

客户端可订阅资源变更通知，以便在资源更新时及时获知：

err := client.Subscribe(ctx, mcp.SubscribeRequest{
    Params: mcp.SubscribeParams{
        URI: "test://resource/path",
    },
})

如需取消资源通知：

err := client.Unsubscribe(ctx, mcp.UnsubscribeRequest{
    Params: mcp.UnsubscribeParams{
        URI: "test://resource/path",
    },
})

-->
                <div id="comment-holder">
                </div>
            </article>
        </div>
        <footer>
             AJ-Util，开源框架 <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> 的一部分。联系方式：
             frank@ajaxjs.com，<a href="https://blog.csdn.net/zhangxin09" target="_blank">作者博客</a>
             <br />
             <br />
             Copyright © 2025 Frank Cheung. All rights reserved.
         </footer>
    </body>
</html>